﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DataModel;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.StandardUI;

namespace QLBH.Category
{
    public partial class AddLyDoDlg : Form
    {
        public eDlgType DlgType { set; get; }
        public LYDO Model { set; get; }
        public AddLyDoDlg()
        {
            InitializeComponent();

            Model = new LYDO();
            Model.ID = Guid.NewGuid();
            Model.CODE = CodeHelper.GetNewCode();
        }

        private void AddLyDoDlg_Load(object sender, EventArgs e)
        {
            if (DlgType == eDlgType.EDIT)
            {
                this.Text = "Sửa lý do";

                UpdateModelToControl();
            }
            else
            {
                this.Text = "Thêm mới lý do";
                tbxMaDVT.Text = Model.CODE;
            }
        }
        private void UpdateModelToControl()
        {
            tbxMaDVT.Text = Model.CODE;
            tbxTenDVT.Text = Model.TEN;
            rtbGhiChu.Text = Model.GHICHU;
        }
        private void UpdateModelFromControl()
        {
            Model.CODE = tbxMaDVT.Text;
            Model.TEN = tbxTenDVT.Text;
            Model.GHICHU = rtbGhiChu.Text;
        }
        private bool CheckValidData()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxMaDVT.Text, "Mã lý do",14);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxMaDVT.SelectAll();
                tbxMaDVT.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(tbxTenDVT.Text, "Tên lý do",50);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxTenDVT.SelectAll();
                tbxTenDVT.Focus();
                return false;
            }
            return true;
        }
        private void AddLyDoDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Control && e.KeyCode == Keys.S)
            {
                //Ctrl+S
                btnSave_Click(sender, e);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValidData())
            {
                try
                {
                    UpdateModelFromControl();
                    if (DlgType == eDlgType.ADD)
                    {
                        Model = LyDoController.GetInstance().Add(Model);
                    }
                    else
                    {
                        LyDoController.GetInstance().Edit(Model);
                    }
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
